package com.mallcai.bigdata.ladon.controller.system;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.controller.system.form.ResForm;
import com.mallcai.bigdata.ladon.dpl.entity.system.Resource;
import com.mallcai.bigdata.ladon.service.system.IResService;
import com.mallcai.bigdata.ladon.utils.MaskUtils;
import com.mallcai.bigdata.ladon.vo.Pair;
import com.mallcai.bigdata.ladon.vo.system.TreeNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-06-20 10:34.
 * Modify:
 */
@RequestMapping("/sys/res")
@RestController
public class ResController {
    @Autowired
    private IResService iResService;

    @RequestMapping("/add")
    public String addUser(@RequestBody @Validated(value = ResForm.AddCheck.class) ResForm form) {
        Resource res = form.toEntity();

        int status = iResService.addResource(res);
        if (status <= 0) {
            throw new BusinessException("添加资源失败");
        }
        return "添加资源成功";
    }

    @RequestMapping("/edit")
    public String editUser(@RequestBody @Validated(value = ResForm.EditCheck.class) ResForm form) {
        Resource res = form.toEntity();

        int status = iResService.updateResource(res);
        if (status <= 0) {
            throw new BusinessException("修改资源失败");
        }

        return "修改资源成功";
    }

    @RequestMapping("/del")
    public String delUser(@RequestBody @Validated(value = ResForm.IdCheck.class) ResForm form) {
        Resource res = form.toEntity();
        int status = iResService.deleteResource(res);
        if (status <= 0) {
            throw new BusinessException("删除资源失败");
        }

        return "删除资源成功";
    }


    @RequestMapping("/getByPage")
    public List<Resource> getByPage(@RequestBody @Validated ResForm form) {
        Map<String, Object> params = form.getParams();
        if (params == null) {
            params = new HashMap<>();
        }

        List<Resource> resources = iResService.getByParams(params);

        Map<Integer, List<Resource>> levelMap = new HashMap<>();
        int maxLevel = 1;
        for (Resource r : resources) {
            int level = r.getLevel();
            if (maxLevel < level) {
                maxLevel = level;
            }

            if (!levelMap.containsKey(level)) {
                levelMap.put(level, new ArrayList<>());
            }
            levelMap.get(level).add(r);
        }

        // 从最深的分支开始向根节点递归
        Map<Long, List<Resource>> preNode = new LinkedHashMap<>();
        for (int i = maxLevel; i > 0; i--) {
            Map<Long, List<Resource>> curNode = new LinkedHashMap<>();

            List<Resource> tmp = levelMap.get(i);
            if (tmp == null) {
                continue;
            }

            for (Resource r : tmp) {
                if (preNode.containsKey(r.getId())) {
                    r.setChildren(preNode.get(r.getId()));
                }
                if (!curNode.containsKey(r.getParentId())) {
                    curNode.put(r.getParentId(), new ArrayList<>());
                }
                curNode.get(r.getParentId()).add(r);
            }

            preNode = curNode;
        }

        // 最终的根节点Id为0, 所以这里只取0
        List<Resource> allGroup = new ArrayList<>();
        preNode.values().forEach(l -> allGroup.addAll(l));
        return allGroup;
    }

    /**
     * 获取父级资源
     *
     * @return List
     */
    @RequestMapping("/getAllParents")
    public List<TreeNodeVo> getAllParents() {
        return iResService.getResTree(true);
    }

    /**
     * 获取整体资源树
     *
     * @return List
     */
    @RequestMapping("/resTree")
    public List<TreeNodeVo> getResTree() {
        return iResService.getResTree(false);
    }

    /**
     * 获取所有权限种类
     *
     * @return List
     */
    @RequestMapping("/allMask")
    public List<Pair<Integer, String>> getAllMask() {
        return MaskUtils.allMask();
    }
}
